package com.jsgt.greekfire.ui.entrust.adapter

import android.content.Context
import android.text.TextUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fhx.core.utils.LogUtils
import com.jsgt.greekfire.ENTRUST_NUM
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.EntrustBean
import com.jsgt.greekfire.common.util.UtilsBigDecimal

class EntrustNodeAdapter(
    context: Context,
    layoutId: Int,
    data: ArrayList<EntrustBean>? = null
) : BaseQuickAdapter<EntrustBean, BaseViewHolder>(layoutId, data) {
    override fun convert(helper: BaseViewHolder, item: EntrustBean) {
        try {
        if (!TextUtils.isEmpty(item.delegate_count)) {
            val div = UtilsBigDecimal.div(item.delegate_count!!, ENTRUST_NUM)
            helper.setText(R.id.tvTotalCommissioned, div.toString())
        }else{
            helper.setText(R.id.tvTotalCommissioned, "--,--")
        }
        if (item.delegateAccount != null && item.delegateAccount!!.isNotEmpty()) {
            if (!TextUtils.isEmpty(item.delegateAccount!![item.delegateAccount!!.size - 1].delegate_count!!)) {
                val minNum = UtilsBigDecimal.div(
                    item.delegateAccount!![item.delegateAccount!!.size - 1].delegate_count!!,
                    ENTRUST_NUM
                )
                helper.setText(R.id.tvMinEntrustment, minNum.toString())
            }else{
                helper.setText(R.id.tvMinEntrustment, "--,--")
            }
            if (!TextUtils.isEmpty(item.delegateAccount!![0].delegate_count!!)) {
                val maxNum =
                    UtilsBigDecimal.div(item.delegateAccount!![0].delegate_count!!, ENTRUST_NUM)
                helper.setText(R.id.tvMaxNumberDelegates, maxNum.toString())
            }else{
                helper.setText(R.id.tvMaxNumberDelegates, "--,--")
            }

            helper.setText(R.id.tvNumberParticipants, item.delegateAccount!!.size.toString())
        }else{
            helper.setText(R.id.tvNumberParticipants, "--,--")
            helper.setText(R.id.tvMinEntrustment, "--,--")
            helper.setText(R.id.tvMaxNumberDelegates, "--,--")
        }
        helper.setText(R.id.tvPosition, "Node" + (helper.adapterPosition + 1))
            .setText(R.id.tvAddress, item.address)

        } catch (e: Exception) {
            LogUtils.error(e.message)
            e.printStackTrace()
        }
    }

}